package org.example.mall.admin.service.manage.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.example.mall.admin.configuration.exception.BaseException;
import org.example.mall.admin.mapper.manage.SysDictItemMapper;
import org.example.mall.admin.mapper.manage.SysDictMapper;
import org.example.mall.admin.model.entity.manage.SysDict;
import org.example.mall.admin.model.entity.manage.SysDictItem;
import org.example.mall.admin.model.enums.manage.DictTypeEnum;
import org.example.mall.admin.service.manage.SysDictItemService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典项值 服务实现类
 * </p>
 *
 * @author hzq
 * @since 2022-01-13
 */
@Service
@AllArgsConstructor
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {

    private final SysDictMapper sysDictMapper;

    @Override
    public void delByDictId(Integer dictId) {
        remove(Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getDictId, dictId));
    }

    @Override
    public List<SysDictItem> listByType(String type) {
        return list(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getType, type));
    }

    @Override
    public Boolean edit(SysDictItem dictItem) {
        SysDict dict = sysDictMapper.selectById(dictItem.getDictId());
        if (DictTypeEnum.SYSTEM.getType().equals(dict.getDictType())) {
            throw new BaseException("系统内置字典项不能被修改");
        }
        return updateById(dictItem);
    }

    @Override
    public Boolean del(Integer dictItemId) {
        SysDictItem dictItem = getById(dictItemId);
        SysDict dict = sysDictMapper.selectById(dictItem.getDictId());
        if (DictTypeEnum.SYSTEM.getType().equals(dict.getDictType())) {
            throw new BaseException("系统内置字典项不能被删除");
        }
        return removeById(dictItemId);
    }
}
